
#import "XMBaseTabBarViewController.h"

#define kNewTabBarHeight kTabBarHeight + 5

@interface XMBaseTabBarViewController () <UITabBarControllerDelegate>

@end


@implementation XMBaseTabBarViewController

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self loadTabBarHeight];
}

- (void)loadTabBarHeight {
    CGRect frame = self.tabBar.frame;
    frame.size.height = kNewTabBarHeight;
    frame.origin.y = self.view.frame.size.height - frame.size.height;
    self.tabBar.frame = frame;
    self.tabBar.barStyle = UIBarStyleBlack;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.delegate = self;
    self.tabBar.translucent = NO;
    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.shadowImage = [UIImage new];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kNewTabBarHeight)];
    [imageView setImage:[self createImageWithColor:[UIColor whiteColor]]];
    [imageView setContentMode:UIViewContentModeScaleToFill];
    [self.tabBar insertSubview:imageView atIndex:0];
//    //覆盖原生Tabbar的上横线
//    [[UITabBar appearance] setShadowImage:[self createImageWithColor:[UIColor clearColor]]];
//    //背景图片为透明色
//    [[UITabBar appearance] setBackgroundImage:[self createImageWithColor:[UIColor clearColor]]];
//    self.tabBar.backgroundColor = [UIColor clearColor];
//    //设置为半透明
//    self.tabBarController.tabBar.translucent = YES;
}

//- (void)loadCAGradientLayer {
//    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"#FFFFFF" alpha:0.f].CGColor, (__bridge id)[UIColor colorWithHexString:@"#FFFFFF " alpha:1.f].CGColor];
//    gradientLayer.locations = @[@0.f, @1.f];
//    gradientLayer.startPoint = CGPointMake(0.f, 0.f);
//    gradientLayer.endPoint = CGPointMake(0.f, 1.f);
//    gradientLayer.frame = CGRectMake(0.f, 0.f, SCREEN_WIDTH, kTabBarHeight);
//    [self.tabBar.layer addSublayer:gradientLayer];
//}

- (UIImage *)createImageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (void)switchTabBarIndex:(NSInteger)index {
    self.selectedIndex = index;
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    [super setSelectedIndex:selectedIndex];
    [self.tabBarView swithItemViewIndexNoBlock:selectedIndex];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return NO;
}

- (void)setTabBarView:(UIView<XMTabBarViewDelegate> *)tabBarView {
    if (_tabBarView != tabBarView) {
        tabBarView.frame = CGRectMake(0.f, 0.f, [UIScreen mainScreen].bounds.size.width, kNewTabBarHeight);
        tabBarView.layer.shadowColor = [UIColor colorWithRed:214.f / 255.f green:214.f / 255.f blue:214.f / 255.f alpha:0.53f].CGColor;
        tabBarView.layer.shadowOffset = CGSizeMake(0, 0);
        tabBarView.layer.shadowOpacity = 1;
        tabBarView.layer.shadowRadius = 16;
        [self.tabBar addSubview:tabBarView];
    }
    _tabBarView = tabBarView;
}

@end
